package com.vcredit.creditcat.service.product;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.dto.product.TransitionInfo;
import com.vcredit.creditcat.model.CustAccount;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.persistence.base.CustAccountMapper;
import com.vcredit.creditcat.persistence.base.CustProductApplyMapper;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.framework.util.VelocityUtil;

@Service("transitionInfo")
public class ITransitionInfoService {

	@Autowired
	private CustAccountMapper custAccountMapper;

	@Value("${xinyongmao.publicKey}")
	private String publicKey;

	private static Logger logger = LoggerFactory.getLogger(ITransitionInfoService.class);

	@Autowired
	private CustProductApplyMapper custProductApplyMapper;

	public List<TransitionInfo> queryTransitionInfo(String productId, long custId, String mobile) throws Exception {
		// TODO Auto-generated method stub

		List<TransitionInfo> list = new ArrayList<TransitionInfo>();
		// 返回手机+密码的引导信息
		if (productId.equals("36") || productId.equals("31")) {
			list = getProduct(productId, custId);
		} else {
			list = getOther(productId, custId);
		}

		if (null == list || list.size() == 0) {
			logger.info("返回引导信息为空");
			// 如果未识别，改为默认失败数据
			return null;
		}

		return list;
	}

	/**
	 * 丽人荟引导信息
	 * 
	 * @param productId
	 * @param custId
	 * @param mobileNo2
	 * @return
	 * @throws Exception
	 */
	private List<TransitionInfo> getProduct(String productId, Long custId) throws Exception {
		List<TransitionInfo> listData = new ArrayList<TransitionInfo>();
		TransitionInfo transitionInfo = new TransitionInfo();

		// 查询手机号码
		CustAccount custAccount = new CustAccount();
		custAccount.setCustId(custId);
		List<CustAccount> list = custAccountMapper.find(custAccount);
		String mobile = ((CustAccount) list.get(0)).getUserName();

		transitionInfo.setProductId(Integer.parseInt(productId));
		transitionInfo.setAccountName(mobile);

		// 查询第3方密码
		CustProductApply custProductApply = new CustProductApply();
		custProductApply.setCustId(custId);
		custProductApply.setProductId(Long.valueOf(productId));
		List list2 = custProductApplyMapper.find(custProductApply);
		if (null == list2 || list2.size() == 0) {
			logger.info("丽人荟查询 custProductApply数据为空");
			return null;
		}
		String pwd = ((CustProductApply) list2.get(0)).getThirdPwd();
		transitionInfo.setAccountPwd(pwd);

		// 加载message
		transitionInfo = loadMessage(transitionInfo, productId, mobile);

		// 加入List集合
		listData.add(transitionInfo);
		return listData;
	}

	private List<TransitionInfo> getOther(String productId, Long custId) throws Exception {
		List<TransitionInfo> listData = new ArrayList<TransitionInfo>();
		TransitionInfo transitionInfo = new TransitionInfo();

		// 查询手机号码
		CustAccount custAccount = new CustAccount();
		custAccount.setCustId(custId);
		List<CustAccount> list = custAccountMapper.find(custAccount);
		String mobile = ((CustAccount) list.get(0)).getUserName();

		transitionInfo.setProductId(Integer.parseInt(productId));
		transitionInfo.setAccountName(mobile);

		// 加载message
		transitionInfo = loadMessage(transitionInfo, productId, mobile);

		// 加入List集合
		listData.add(transitionInfo);
		return listData;
	}

	private TransitionInfo loadMessage(TransitionInfo transitionInfo, String productId, String mobile)
			throws Exception {

		// 组装并设置message
		String message = "";
		if (productId.equals("25")) {// 信而富
			message = VelocityUtil.templateToText("template/productStatus09.vm", "mobile", mobile);
		} else if (productId.equals("6")||productId.equals("3")) {// 信用花
			message = VelocityUtil.templateToText("template/productStatus10.vm", "mobile", mobile);
		} else if (productId.equals("36")) {// 丽人荟
			message = VelocityUtil.templateToText("template/productStatus11.vm", "mobile", mobile);
		} else if (productId.equals("31")) {// 还乐
			message = VelocityUtil.templateToText("template/productStatus12.vm", "mobile", mobile);
		}
		if (message.trim().length() > 0) {
			message = message.replace("\r\n", "");
			transitionInfo.setMessage(message);
		}
		return transitionInfo;
	}

}
